/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author tea milandi
 */
public class User {

    private String idUser;
    private String namaUser;
    private String Status;
    private String username;
    private String password;
    String jdbcUrl = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
    String userid = "mhs115314008"; // gantikan dengan username anda
    String pass = "mhs115314008"; // gantikan dengan password anda

    public String getIdUser() {
        return idUser;
    }

    public void setIdUser(String idUser) {
        this.idUser = idUser;
    }

    public String getNamaUser() {
        return namaUser;
    }

    public void setNamaUser(String namaUser) {
        this.namaUser = namaUser;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String Status) {
        this.Status = Status;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean bandingUser() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select username, password from user_asmart "
                    + "where username = '"
                    + getUsername() + "' and password = "
                    + "'" + getPassword() + "'");
            while (resultSet.next() == true) {
                return true;
            }
            return false;
        } catch (SQLException e) {

            System.out.println("error :" + e.getMessage());
            return false;
        }
    }

    public String bandingJenisUser() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            String hasil = null;
            ResultSet resultSet = statement.executeQuery("select jenisuser from user_asmart where username = '" + getUsername() + "'");
            while (resultSet.next()) {
                if (resultSet.getString("jenisuser") == null) {
                    hasil = "0";
                } else {
                    hasil = resultSet.getString("jenisuser");
                }
            }
            if ("0".equals(hasil)) {
                return "0";
            } else {
                setStatus(hasil);
                return hasil;
            }
        } catch (SQLException e) {

            System.out.println("error :" + e.getMessage());
            return "0";
        }
    }

    public void tambahUser(String username, String password, String jenisuser) throws SQLException {
        Connection connection = null;
        OracleDataSource ds;
        ds = new OracleDataSource();
        ds.setURL(jdbcUrl);
        ds.setPassword(pass);
        ds.setUser(userid);
        connection = (Connection) ds.getConnection();
        Statement st = connection.createStatement();
        st.executeQuery("insert into user_asmart values ('"
                + username + "','"
                + username + "','"
                + jenisuser+ "','"
                + password + "')");
    }
}